package com.leyou.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leyou.auth.dto.UserDetail;
import com.leyou.auth.utils.UserContext;
import com.leyou.commons.exception.LyException;
import com.leyou.user.entity.*;
import com.leyou.user.mapper.AddressMapper;
import com.leyou.user.service.AddressService;
import com.leyou.user.service.AreaService;
import com.leyou.user.service.CityService;
import com.leyou.user.service.ProvinceService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class AddressServiceImpl extends ServiceImpl<AddressMapper,Address> implements AddressService {

    @Override
    public void saveAddress(Address address) {
        UserDetail user = UserContext.getUser();
        address.setUserId(user.getId());
        this.save(address);
    }
    @Autowired
    private ProvinceService provinceService;
    @Autowired
    private CityService cityService;
    @Autowired
    private AreaService areaService;
    @Override
    public  List<AddressDTO> findByUid() {
        //通过uid查询地址
        Long id = UserContext.getUser().getId();
        if(id==null) throw new LyException(401,"未登录");
        List<Address> addresses = this.query().eq("user_id", id).list();
        List<AddressDTO> addressDTOList = addresses.stream().map(address -> {
            AddressDTO addressDTO = buildAddress(address);
            return addressDTO;
        }).collect(Collectors.toList());

        return addressDTOList;
    }

    private AddressDTO buildAddress(Address address) {

        Province province = provinceService.query().eq("provinceid", address.getProvince()).one();
        City city = cityService.query().eq("cityid", address.getCity()).one();
        Area area = areaService.query().eq("areaid", address.getDistrict()).one();
        AddressDTO addressDTO = new AddressDTO();
        BeanUtils.copyProperties(address,addressDTO);
        addressDTO.setProvinceName(province.getProvince());
        addressDTO.setCityName(city.getCity());
        addressDTO.setDistrictName(area.getArea());
        return addressDTO;
    }

    @Override
    public void delete(Long id) {
        if (id==null) throw new LyException(401,"id不能为空");
        QueryWrapper<Address> wrapper = new QueryWrapper<Address>()
                .eq("id", id)
                .eq("user_id", UserContext.getUser().getId());
        boolean remove = this.remove(wrapper);
        if(remove) System.out.println("删除成功");
        else System.out.println("删除失败");
    }

    @Override
    public AddressDTO findById(Long id) {
        Address address = this.query().eq(id!=null,"id", id)
                .eq(UserContext.getUser().getId()!=null,"user_id", UserContext.getUser().getId())
                .one();
        AddressDTO addressDTO = buildAddress(address);
        return addressDTO;
    }

    @Override
    public void updateAddress(Address address) {
        this.update(address,new QueryWrapper<Address>()
                .eq("id",address.getId())
                .eq("user_id",UserContext.getUser().getId()));
    }

    @Override
    public void defaultAddress(Long id) {
        Address address = new Address();
        address.setIsDefault(true);

        this.update(address,new QueryWrapper<Address>()
                .eq("id",id)
                .eq("user_id",UserContext.getUser().getId()));
    }

}
